import { Component, OnInit, ViewChild } from '@angular/core';
import { DatePipe } from '@angular/common';
import { NzMessageService } from 'ng-zorro-antd';
import { Router, NavigationEnd } from '@angular/router';
import { environment } from '../../../../environments/environment';
import { TableComponent } from './table.component';
import { _HttpClient } from '@delon/theme';
import { ExportFileService } from '../../../core/exportFile';
import { LocalStorage } from '@core/local.storage';

@Component({
    selector: 'app-workareaweave',
    templateUrl: './workAreaWeave.component.html',
    styleUrls: ['./workAreaWeave.component.less']
})

export class WorkAreaWeaveComponent implements OnInit {
    @ViewChild('tcp', { static: true }) tcp: TableComponent;
    searchParam: any = {};
    apiUrl: any = {};
    tableScroll: any = {};
    exportUrl = '';
    headData = [];
    plainData = [];
    tempData = [];
    operaList: any = [
        { addModal: false },
        { delModal: false },
    ];
    workshopSelect = [];
    statusSelect = [];
    paramType = '';
    paramOver = '';
    tabIndex = 0;
    orgid = JSON.parse(this.ls.get('loginInfo')).VC_ORG$ID;
    level = JSON.parse(this.ls.get('loginInfo')).N_LEVEL;
    treeNodeStyle = { 'max-height': '300px', 'overflow-y': 'auto', 'overflow-x': 'hidden' };
    typeName = '';
    ztList = [
        { "opValue": "0", "opName": "编辑" },
        { "opValue": "1", "opName": "提交" },
        { "opValue": "2", "opName": "车间批准" },
        { "opValue": "3", "opName": "车间驳回" },
        { "opValue": "4", "opName": "段批准" },
        { "opValue": "5", "opName": "段驳回" }
    ];
    cjList = [];

    constructor(
        private exportFile: ExportFileService,
        private msg: NzMessageService,
        public ls: LocalStorage,
        private router: Router,
        private http: _HttpClient,
        private datePipe: DatePipe
    ) {
        this.router.events.subscribe((event: any) => {
            if (event instanceof NavigationEnd) {
                if (event.url === '/workAreaWeave/workarea') {
                    this.loadList();
                }
            }
        });
    }

    ngOnInit() {
        this.typeName = window.location.href.split('/')[window.location.href.split('/').length - 1];
        switch (this.typeName) {
            case 'seg':
                this.headData = [
                    { "name": "车间" },
                    { "name": "月份", "width": "10%" },
                    { "name": "周次", "width": "10%" },
                    { "name": "日期范围", "width": "10%" },
                    { "name": "状态", "width": "10%" },

                    { "name": "录入人", "width": "10%" },
                    { "name": "录入时间", "width": "10%" }
                ];
                this.plainData = [
                    "wsorgname", "vcYearMonth", "vcWeeknum", "sedate", "status",
                    "vcUserid", "dtEditdate"
                ];
                break;
            default:
                this.headData = [
                    { "name": "车间" },
                    { "name": "工区", "width": "10%" },
                    { "name": "月份", "width": "8%" },
                    { "name": "周次", "width": "5%" },
                    { "name": "日期范围", "width": "10%" },

                    { "name": "状态", "width": "8%" },
                    { "name": "车间审批人", "width": "8%" },
                    { "name": "段审批人", "width": "8%" },
                    { "name": "段审时间", "width": "10%" },
                    { "name": "录入人", "width": "8%" },

                    { "name": "录入时间", "width": "10%" }
                ];
                this.plainData = [
                    "wsorgname", "waorgname", "vcYearMonth", "vcWeeknum", "sedate",
                    "status", "vcWsappper", "", "vcSegappper", "vcUserid",
                    "dtEditdate"
                ];
                break;
        }

        this.apiUrl.add = environment.project + '/ymTWaweekplanmkm/add';
        this.apiUrl.del = environment.project + '/ymTWaweekplanmkm/delete';
        this.apiUrl.tb = environment.project + '/ymTWaweekplanmkm/update';
        this.apiUrl.approve = environment.project + '/ymTWaweekplanmkm/update';
        this.apiUrl.upload = environment.project + '/ymTWaweekplanmkm/upload';
        this.http.get(environment.apiBase + '/csmis/getAllChejian.jwt').subscribe(res => {
            this.cjList = res;
        })
        this.loadList();
    }
    loadList() {
        if (this.level === 10) {
            this.tcp.loadList(environment.project + '/ymTWaweekplanmkm/query', '&vcOrgid=' + this.orgid);
        } else if (this.level === 30) {
            this.tcp.loadList(environment.project + '/ymTWaweekplanmkm/wsQuery', '&vcOrgid=' + this.orgid);
        } else {
            this.tcp.loadList(environment.project + '/ymTWaweekplanmkm/wsQuery', '');
        }
    }
    search() {
        let params = '';
        if (this.searchParam.vcMonth) {
            params += '&vcYear=' + this.datePipe.transform(this.searchParam.vcMonth, 'yyyy');
            params += '&vcMonth=' + this.datePipe.transform(this.searchParam.vcMonth, 'M');
        }
        if (this.level === 10 || this.level === 30) {
            params += '&vcOrgid=' + this.orgid;
        }
        if (this.searchParam.wsOrgId) {
            params += '&wsOrgId=' + this.searchParam.wsOrgId;
        }
        if (this.searchParam.vcWeeknum) {
            params += '&vcWeeknum=' + this.searchParam.vcWeeknum;
        }
        if (this.searchParam.vcStatus) {
            params += '&vcStatus=' + this.searchParam.vcStatus;
        }
        this.exportUrl = params;
        this.tcp.loadList(environment.project + '/ymTWaweekplanmkm/wsQuery', params);
    }
    export() {
        let status = false;
        let count = 0;
        const vcId = [];
        for (const item of this.tcp.getAllData()) {
            if (item.checked) {
                count += 1;
                vcId.push(item.vcId);
                status = true;
            }
        }
        if (status && count === 1) {
            this.exportFile.getDown(environment.project + '/ymTWaweekplanmkm/export?vcId=' + vcId[0], '周计划.xls');
        } else {
            this.msg.error('请选择一条数据进行操作');
        }
    }
    modalShow(type) {
        this.tempData = this.tcp.getAllData();
        this.operaList[type] = true;
    }
    modalHide(type) {
        this.operaList[type] = false;
    }
}
